<template>
         <h2>添加理财信息</h2>
         <table >
             <tr>
                 <td>理财名称</td>
                 <td><input type="text" v-model="info.F_name"></td>
             </tr>
             <tr>
                 <td>年收益</td>
                 <td><input type="text" v-model="info.F_APY"></td>
             </tr>
             <tr>
                 <td>理财最低金额</td>
                 <td><input type="text" v-model="info.F_MinMoney"></td>
             </tr>
             <tr>
                 <td>风险等级</td>
                 <td>
                    <input type="radio" name="Flevel" value="0" v-model="info.F_Level" checked>低风险
                    <input type="radio" name="Flevel" value="1" v-model="info.F_Level">中风险
                    <input type="radio" name="Flevel" value="2" v-model="info.F_Level">高风险
                 </td>
             </tr>
             <tr>
                 <td>理财利率</td>
                 <td>
                    <select v-model="info.I_id">
                        <option value="0">==请选择==</option>
                        <option v-for="item in interestInfo" :value=item.I_id>{{item.I_year}}年利率为{{item.I_Interest}}%</option>
                    </select>
                 </td>
             </tr>
             <tr>
                <td colspan="2" style="text-align: center;">
                    <input type="button" value="保存" class="btn btn-primary" @click="SaveData">
                </td>
             </tr>
         </table>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue'
import axios from 'axios'

import {useRouter} from 'vue-router'

const router = useRouter()

//创建响应式数据
const info = ref({
    F_name:"",
    F_APY:"",
    F_MinMoney:"",
    F_Number:"",
    F_Level:0,
    I_id:0
})

//创建生命周期函数勾
onMounted(()=>{
    LoadInterestInfo();
})

//创建理财利息响应式数据
const interestInfo = ref([{
     I_id:0,
     I_year:0,
     I_Interest:0
}])

const LoadInterestInfo=()=>{
     axios.get("https://localhost:44337/api/Finans/ShowInterestInfo").then(res=>{
        interestInfo.value=res.data;
         console.log(res.data)
     }).catch(err=>{
         console.log(err)
     })
}

const SaveData=()=>{
     //验证非空
     if(info.value.F_name=="")
     {
        alert('理财项目名称不能为空!');
        return;
     }
     
     //验证理财最低金额需大于0元且小于1亿元
     if(Number(info.value.F_MinMoney)<=0 || Number(info.value.F_MinMoney)>=100000000)
     {
        alert('理财最低金额需大于0元且小于1亿元!');
        return;
     }

     axios.post("https://localhost:44337/api/Finans/AddFinanInfo",info.value).then(res=>{
         console.log(res.data)
         if(res.data>0)
         {
            alert('理财项目信息添加成功!');
            router.push('/ShowFinanInfo')
         } else if(res.data==-1)
         {
            alert('理财项目名称已存在!请跟换');
         }
         else
         {
            alert('理财项目信息添加失败!');
         }
     })
}
</script>